package org.wlgzs.shop.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wlgzs.shop.bean.*;
import org.wlgzs.shop.dao.OrderTransportDataMapper;
import org.wlgzs.shop.service.ICarDataService;
import org.wlgzs.shop.service.IGoodsDateService;
import org.wlgzs.shop.service.IOrderTransportDateService;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * @Author: Ben
 * @Date: 2019/5/20 8:58
 * @Project org.wlgzs.shop
 */
@Service(value = "OrderTransportDateService")
public class OrderTransportDateServiceImpl implements IOrderTransportDateService {
   @Autowired
    OrderTransportDataMapper orderTransportDataMapper;
   @Autowired
    IGoodsDateService goodsDateService;
   @Autowired
    ICarDataService  carDataService;
   @Autowired
   IOrderTransportDateService iOrderTransportDateService;



   Logger logger= LoggerFactory.getLogger(OrderTransportDateServiceImpl.class);

    /**
     * 增加订单信息
     * @param orderTransportData
     * @return int
     */
    @Override
    public int insertOrderTransportDataService(OrderTransportData orderTransportData) {
        return orderTransportDataMapper.insert(orderTransportData);
    }

    /**
     *根据订单id删除订单
     * @param orderId
     * @return int
     */
    @Override
    public int deleteOrderTransportDataService(int orderId) {
        return  orderTransportDataMapper.deleteByPrimaryKey(orderId);
    }


    /**
     * 根据订单id更改订单信息
     * @param orderTransportData
     * @return int
     */
    @Override
    public int updateOrderTransportDataService(OrderTransportData orderTransportData) {
        return  orderTransportDataMapper.updateByPrimaryKeySelective(orderTransportData);
    }

    /**
     *根据用户id查询订单信息
     * @param userId
     * @return List<OrderTransportData>
     */
    @Override
    public List<OrderTransportData> selectUserIdOrderTransportDateService(int userId) {
        OrderTransportDataExample orderTransportDataExample=new OrderTransportDataExample();
        OrderTransportDataExample.Criteria criteria=orderTransportDataExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andEndTimeIsNull();
        List<OrderTransportData> orderTransportDataList=orderTransportDataMapper.selectByExample(orderTransportDataExample);
        return orderTransportDataList;
    }

    /**
     *根据订单id查询订单信息
     * @param orderId
     * @return List<OrderTransportData>
     */
    @Override
    public OrderTransportData selectOrderIdOrderTransportDateService(int orderId) {
        return orderTransportDataMapper.selectByPrimaryKey(orderId);
    }

    //获取所有订单信息
    @Override
    public List<OrderTransportData> allOrder() {
        return orderTransportDataMapper.selectByExample(null);
    }

    @Override
    public Msg addAllOrder(List<String> date, HttpSession session) {
        UserDate userDate=(UserDate)session.getAttribute("user");
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar=Calendar.getInstance();
        String timeDate=simpleDateFormat.format(calendar.getTime());
        List<OrderTransportData> OrderDate=new ArrayList<OrderTransportData>();
        float allPerice=0;
        for (String order:date){
            String[] orderDate=order.split(",");
            System.out.println(orderDate[0]);
            int number=Integer.parseInt(orderDate[1]);
            GoodsData data=goodsDateService.selectGoods(Integer.parseInt(orderDate[0]));
            float onePerice=data.getGoodsPrice()*number;
            allPerice+=onePerice;
            OrderTransportData orderTransportData=new OrderTransportData((data.getGoodsPrice()*number),false,timeDate,userDate.getUserId(),Integer.parseInt(orderDate[1]),data.getGoodsPrice(),data.getPhotoUrl(),data.getGoodsName(),data.getId(),"0");
            carDataService.deleteCar(data.getId()+"",userDate.getUserId());
            OrderDate.add(orderTransportData);
            int select=orderTransportDataMapper.insertSelective(orderTransportData);
            if(select==0){
                return Msg.fail();
            }
            logger.info("完成！！！");
        }
        session.setAttribute("orderDate",OrderDate);
        session.setAttribute("money",allPerice);
        return Msg.success();
    }

    @Override
    public List<OrderTransportData> selectOrderByUser(int userId) {
        OrderTransportDataExample orderTransportDataExample=new OrderTransportDataExample();
        OrderTransportDataExample.Criteria criteria=orderTransportDataExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return orderTransportDataMapper.selectByExample(orderTransportDataExample);
    }

    @Override
    public boolean setOrderStatus() {
        orderTransportDataMapper.selectByExample(null);
        for( OrderTransportData orderTransportData:orderTransportDataMapper.selectByExample(null)) {
            if(!orderTransportData.getStatus()) {
                orderTransportData.setStatus(true);
                orderTransportDataMapper.updateByPrimaryKey(orderTransportData);
            }
        }
        return true;
    }

    public boolean setPostion(String postion){
        orderTransportDataMapper.selectByExample(null);
        for( OrderTransportData orderTransportData:orderTransportDataMapper.selectByExample(null)) {
            if(orderTransportData.getBuyerMessage()==null||orderTransportData.getBuyerMessage().equals("")) {
                orderTransportData.setBuyerMessage(postion);
                orderTransportDataMapper.updateByPrimaryKey(orderTransportData);
            }
        }
        return true;
    }

    @Override
    public boolean upDatePostStatus(List<OrderTransportData> list)
    {   int number=0;
        for(OrderTransportData orderTransportData:list) {
        number=orderTransportDataMapper.updateByDate(orderTransportData);
        }
        if(number!=0) {
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public boolean addPostDate(List<OrderTransportData> list) {
        int number=0;
        for(OrderTransportData orderTransportData:list) {
            System.out.println(orderTransportData.toString());
            number=orderTransportDataMapper.upTwoDateByDate(orderTransportData);
        }
        if(number!=0) {
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public List<OrderTransportData> selectOrder(String date) {
       OrderTransportDataExample orderTransportDataExample=new OrderTransportDataExample();
       OrderTransportDataExample.Criteria criteria=orderTransportDataExample.createCriteria();
       criteria.andShippingNameLike("%"+date+"%");
       orderTransportDataExample.or().andConsignTimeLike("%"+date+"%");
       return orderTransportDataMapper.selectByExample(orderTransportDataExample);    }

    @Override
    public void endOrder(int orderID) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar=Calendar.getInstance();
        String endTime=simpleDateFormat.format(calendar.getTime());
        System.out.println(orderID);
        orderTransportDataMapper.upDateEndTime(endTime,orderID+"");
    }

    @Override
    public List<OrderTransportData> selectUserIdFinishOrderTransportDateService(int id) {
        OrderTransportDataExample orderTransportDataExample=new OrderTransportDataExample();
        OrderTransportDataExample.Criteria criteria=orderTransportDataExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andEndTimeIsNotNull();
        List<OrderTransportData> orderTransportDataList=orderTransportDataMapper.selectByExample(orderTransportDataExample);
        return orderTransportDataList;
    }

}
